use play_api::*;
use sha1::{Digest, Sha1};
use std::io::Read;
pub struct FileChecker;
impl FileCheck for FileChecker {
    type Error = std::io::Error;
    fn check_file(&self, file: &GameFile) -> Result<bool, Self::Error> {
        let mut hasher = Sha1::new();
        let local_file = std::fs::File::open(file.path.clone())?;
        let mut reader = std::io::BufReader::new(local_file);
        let mut buffer = [0; 1024];
        while let Ok(n) = reader.read(&mut buffer) {
            if n == 0 {
                break;
            }
            hasher.update(&buffer[..n]);
        }
        let hash = hasher.finalize();
        let hash_string = base16ct::lower::encode_string(&hash);
        Ok(hash_string == file.sha1)
    }
    fn check_notmatch_file(&self, files: &[GameFile]) -> Result<Vec<GameFile>, Self::Error> {
        let mut notmatch = Vec::new();
        for f in files {
            if !self.check_file(f)? {
                notmatch.push(f.clone());
            }
        }
        Ok(notmatch)
    }
}
